<style type="text/css">
h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
code { color: #333366; font-style: italic; }
code.opt { font-weight: bold; font-style: normal; }
.error { color: #993333; }
.warning { color: #cc33cc; }
/*
 * backport classes from WP 1.5.1
 */
 .active td {background: #BEB;}
 .active .name {background: #9C9;}
 .alternate.active td {background: #ADA;}
 .alternate.active .name {background: #8B8;}
</style>
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('admin.php');

$title = 'Plugins';
$this_file = 'lightpress/wp-plugin/plugins.php';
$parent_file = 'lightpress/wp-plugin/plugins.php';

$submenu[$this_file][] = array(
    'Plugins',
    6,
    'lightpress/wp-plugin/plugins.php',
    'LightPress Plugins'
    );

require_once 'LightPressOptions.php';

$lp_opts = new LightPressOptions();

?>
<div class="wrap">

<?php

if (is_null($basedir = $lp_opts->basedir)) {
    echo '<h3 class="error">LightPress configuration not found</h3>please <a href="admin.php?page=lightpress/wp-plugin/main.php">check the folder where you have installed LightPress</a>, then set (and save) required options';
} else if (!@include_once "$basedir/classes/Frontend.php") {
    echo '<h3 class="error">classes/Frontend.php not found in the LightPress installation directory</h3>please <a href="admin.php?page=lightpress/wp-plugin/main.php">check the folder where you have install LightPress</a>';
} else if (!@is_dir("$basedir/plugins") || !@is_readable("$basedir/plugins")) {
    echo '<h3 class="error">plugin folder not found in the LightPress installation directory</h3>please <a href="admin.php?page=lightpress/wp-plugin/main.php">check the folder where you have install LightPress</a>';
} else {
    // get plugin configuration
    if (!($plugins_config = get_option('lp_opt_plugins')))
        $plugins_config =& $lp_opts->plugins;       // use default plugins
    
    // retrieve plugin files
    
    $dummy_obj = array();
    $plugins = array();
    
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $basedir);

    foreach ($lp_opts->getPluginList() as $plugin_file) {
        if (!include_once("$basedir/plugins/$plugin_file"))
            continue;
        $plugin_name = substr($plugin_file, 0, -4);
        $plugin =& new $plugin_name($dummy_obj, $dummy_obj, true);
        $plugins[$plugin_name] =& $plugin;
    }
    
    // retrieve contexts and their constants
    $contexts = array();
    foreach (explode(' ', LP_CONTEXTS) as $c) {
        $context = strtoupper("LP_CONTEXT_$c");
        if (defined($context))
            $contexts[$c] = constant($context);
    }
    
    // retrieve the action, if there is one
    $action = (isset($_POST['action']) ? $_POST['action'] : '');
    
    // check and retrieve the plugin name, if we have an action
    // hairy code following, it must be the effect of running inside WP :)
    if (!empty($action) // we have an action
        &&              // and
        (
            !isset($_POST['lp_plugin'])                                 // the plugin name is not set
            ||                                                          // or
            !isset($plugins[($plugin_name=$_POST['lp_plugin'])])        // the plugin name is invalid
         )
    ) {
        // display an error message and stop processing
        echo '<h2>LightPress Plugins</h2>';
        echo '<div class="error" style="margin-bottom: 6px;">No plugin to manage</div>';
    } else {
        
        // either we have no action, or we have both an action and a plugin name
        
        // define the message variable we will use as a flag to signal if we have
        // to display the plugin list after processing the actions
        $message = '';
        
        // prepare accessory variables
        
        if ($action) {
            // retrieve plugin information
            if (isset($plugins_config[$plugin_name])) {
                list($plugin_context, $plugin_args) = $plugins_config[$plugin_name];
            } else {
                $plugin_context = 0;
                $plugin_args = array();
            }
            
            $plugin =& $plugins[$plugin_name];
            
            // fill in missing plugin args with defaults
            foreach ($plugin->constructor_args as $k=>$v) {
                if (!isset($plugin_args[$k]))
                    $plugin_args[$k] = $plugin->$k;
            }
            
            // kill old args
            foreach($plugin_args as $k=>$v) {
                if (!isset($plugin->constructor_args[$k]))
                    unset($plugin_args[$k]);
            }
        }
        
        switch ($action) {
            
            case __('Manage','lp'):
                // deal with the manage request
            
                // show form
                echo "<h2>$plugin_name</h2>";
                echo '<div  style="margin-bottom: 6px;">' . $plugin->description . "</div>\n";
                
                echo '<form action="' . $_SERVER['REQUEST_URI'] . '#' . $plugin_name . '" method="post">';
                echo '<input type="hidden" name="lp_plugin" value="' . $plugin_name . '" />';
                
                echo '<table width="100%" cellspacing="3" cellpadding="3">';
                
                echo '<tr><td width="50%" valign="top"><fieldset class="options"><legend>'.__('Plugin Options','lp').'</legend>';
                
                // show plugin args
                if (count($plugin_args) == 0) {
                    echo '<p>'.__('This plugin has no customization options','lp').'</p>';
                } else {
                    foreach ($plugin_args as $arg_name=>$arg_value) {
                        echo '<div style="margin-bottom: 6px;">';
                        echo '<strong>' . $arg_name . '</strong><br />';
                        if (version_compare(PHP_VERSION, "4.1.0", ">=")) {
                            $arg_desc = htmlspecialchars($plugin->constructor_args[$arg_name], ENT_COMPAT, get_option('blog_charset'));
                        } else {
                            $arg_desc = htmlspecialchars($plugin->constructor_args[$arg_name]);
                        }
                        if (is_bool($arg_value)) {
                            echo '<input type="radio" name="lp_arg_' . $arg_name . '" ' .
                                'value="1" ' . ($arg_value ? 'checked="on" ' : '') . '/>&nbsp;'.__('Yes','lp').'&nbsp;';
                            echo '<input type="radio" name="lp_arg_' . $arg_name . '" ' .
                                'value="" ' . (!$arg_value ? 'checked="on" ' : '') . '/>&nbsp;'.__('No','lp');
                        } else {
                            if (version_compare(PHP_VERSION, "4.1.0", ">=")) {
                                $arg_value = htmlspecialchars($arg_value, ENT_COMPAT, get_option('blog_charset'));
                            } else {
                                $arg_value = htmlspecialchars($arg_value);
                            }
                            if (strlen($arg_value) >= 50 || (isset($plugin->textareas) && in_array($arg_name, $plugin->textareas))) {
                                echo '<textarea name="lp_arg_' . $arg_name . '" cols="60" rows="5" wrap="virtual">' .
                                    $arg_value .
                                    '</textarea>';
                            } else {
                                echo '<input type="text" name="lp_arg_' . $arg_name . '" ' .
                                    'value="' . $arg_value . '" size="60" />';
                            }
                        }
                        echo '<br />';
                        echo "<em>$arg_desc</em>";
                        echo '</div>';
                    }
                }
                
                echo '</fieldset></td>';
                
                // show contexts
                
                echo '<td width="50%" valign="top"><fieldset class="options"><legend>'.__('active contexts','lp').'</legend>';
                
                foreach ($contexts as $context_name=>$context_value) {
                    
                    $active_context = ($context_value & $plugin_context);
                    
                    echo '<div style="margin-bottom: 6px;' . ($active_context ? '' : ' color: #999999;') . '">';
                    echo '<input type="checkbox" name="lp_context[]" ' .
                        'value="' . $context_value . '" ' .
                        ($active_context ? 'checked="on" ' : '') .
                        '/>&nbsp;';
                    echo "<strong>$context_name</strong>" .
                        (($context_value & $plugin->default_context) ? '&nbsp;('.__('default','lp').')' : '') .
                        '</div>';
                    
                }
                
                echo '</fieldset></td></tr>';
                
                echo '</table>';
                echo '<input type="submit" name="action" value="'.__('Save', 'lp').'" />';
                echo '</form>';
                
                break;
                
            case __('Activate','lp'):
                
                // update plugin args
                
                if (!isset($plugins_config[$plugin_name])) {
                    $plugin_args = array();
                    foreach ($plugin->constructor_args as $k=>$v)
                        $plugin_args[$k] = $plugin->$k;
                    $plugins_config[$plugin_name] = array(0, $plugin_args);
                }
                
                $err = $plugin->install();
                
                if ($err) {
                    $message = sprintf(__('Error activating %s: %s','lp'), $plugin_name, $err);
                } else {
                    $message = sprintf(__('Plugin %s activated','lp'),$plugin_name);
                    // update plugin context
                    $plugins_config[$plugin_name][0] = $plugin->default_context;
                }
                
                // store plugins configuration
                $lp_opts->updateOption('lp_opt_plugins', $plugins_config);
                
                break;
            
            case __('Deactivate','lp'):
                
                if (isset($plugins_config[$plugin_name])) {
                    $plugins_config[$plugin_name][0] = 0;
                }
                
                $lp_opts->updateOption('lp_opt_plugins', $plugins_config);
                
                $message = sprintf(__('Plugin %s deactivated','lp'),$plugin_name);
                
                break;
                
            case __('Save', 'lp'):
                // deal with the save request
                
                // retrieve plugin args
                
                foreach ($_POST as $k=>$v) {
                    // PHP on some systems has the nasty habit of adding slashes
                    // to POST variable values with magic_quotes_gpc turned off
                    // while the same values in _REQUEST have no slashes
                    if (substr($k, 0, 7) == 'lp_arg_') {
                        $arg_name = substr($k, 7);
                        // check that it's a valid arg
                        if (isset($plugin_args[$arg_name])) {
                            // check and optionally convert its type
                            if (is_bool($plugin->$arg_name))
                                $plugin_args[$arg_name] = (bool)$v;
                            else if (is_float($plugin->$arg_name))
                                $plugin_args[$arg_name] = (float)$v;
                            else if (is_int($plugin->$arg_name))
                                $plugin_args[$arg_name] = (int)$v;
                            else
                                $plugin_args[$arg_name] = stripslashes($v);
                        }
                    }
                }
                
                if (!isset($plugins_config[$plugin_name]))
                    $plugins_config[$plugin_name] = array(0, $plugin_args);
                else
                    $plugins_config[$plugin_name][1] = $plugin_args;
                
                // deal with contexts
                
                $plugin_context = 0;    // if it's not in POST it means we have to clear all contexts
                
                if (isset($_POST['lp_context']) && is_array($_POST['lp_context'])) {
                    foreach ($_POST['lp_context'] as $c)
                        $plugin_context |= (int)$c;
                }
                
                $message = sprintf(__('Configuration for plugin %s updated.','lp'),$plugin_name);

                // check if the context has been modified
                if ($plugin_context > 0 && $plugin_context != $plugins_config[$plugin_name][0]) {
                    // run plugin installation routine
                    $err = $plugin->install();
                    if ($err)
                        $message = sprintf(__('Error activating %s: %s','lp'),$plugin_name,$err);
                    else
                        $plugins_config[$plugin_name][0] = $plugin_context;
                } else {
                    $plugins_config[$plugin_name][0] = $plugin_context;
                }

                $lp_opts->updateOption('lp_opt_plugins', $plugins_config);
                
                break;
                
            default:
                break;
        }
        
        if (!empty($message) || empty($action)) {
            // display the plugin list if we have no action, or if we have successfully
            // completed one of the 'transparent' actions (save, activate, deactivate)
            
            if (!empty($message))
                echo '<div class="updated fade"><p><strong>' . $message . '.</strong></p></div>';
            
            echo '<h2>LightPress Plugins</h2>';
            ?>
            <table width="100%" cellspacing="3" cellpadding="3">
            <?php
    
            foreach ($plugins as $k=>$v) {
                if (isset($plugins_config[$k]) && ($plugins_config[$k][0] > 0)) {
                    $active_for = array();
                    foreach ($contexts as $c=>$n) {
                        if (($n & $plugins_config[$k][0]))
                            $active_for[] = $c;
                    }
                    echo '<tr id="' . $k . '" class="active" title="active for: ' . implode(', ', $active_for) . '"><th class="name"><a name="' . $k . '"></a>' . $k . '</th>' .
                        '<td class="desc" width="100%">' . htmlentities($v->description) . '</td>' .
                        '<td class="togl" nowrap="nowrap">' . 
                        '<form action="' . $_SERVER['REQUEST_URI'] . '#' . $k . '" method="post">' .
                        '<input type="hidden" name="lp_plugin" value="' . $k . '" />' .
                        '<input type="submit" name="action" value="'.__('Deactivate','lp').'" style="font-size:11px;display:block;" /></td><td>' .
                        '<input type="submit" name="action" value="'.__('Manage','lp').'" style="font-size:11px;" />' .
                        '</form></td></tr>';
                } else {
                    echo '<tr id="' . $k . '" class="alternate"><th class="name"><a name="' . $k . '"></a>' . $k . '</th>' .
                        '<td class="desc" width="100%">' . htmlentities($v->description) . '</td>' .
                        '<td class="togl" nowrap="nowrap">' . 
                        '<form action="' . $_SERVER['REQUEST_URI'] . '#' . $k . '" method="post">' .
                        '<input type="hidden" name="lp_plugin" value="' . $k . '" />' .
                        '<input type="submit" name="action" value="'.__('Activate','lp').'" style="font-size: 11px;display:block;" /></td><td>' .
                        '<input type="submit" name="action" value="'.__('Manage','lp').'" style="font-size: 11px;" />' .
                        '</form></td></tr>';
                }
            }
    
            ?>    
            </table>
    
            <?php
        }
    }
}

?>
    
</div>
